//
//  UPOrderProductDetailViewController.m
//  LoveFreshBee
//
//  Created by wyzc on 16/6/14.
//  Copyright © 2016年 叶落知秋. All rights reserved.
//

#import "UPOrderProductDetailViewController.h"
#import "UPOrderProductModel.h"
#import "UPVButton.h"

@interface UPOrderProductDetailViewController ()<UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (weak, nonatomic) IBOutlet UIButton *reduceButton;
@property (weak, nonatomic) IBOutlet UIButton *shopCartButton;
@property (weak, nonatomic) IBOutlet UILabel *numberLabel;

@property (weak, nonatomic) IBOutlet UIButton *increaseButton;
@property (weak, nonatomic) IBOutlet UIProgressView *progessView;

@property(nonatomic,strong)NSTimer *timer;
@end

@implementation UPOrderProductDetailViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
   // self.navigationController.navigationBar.barTintColor=UPColor(255 , 255, 255);
    self.navigationItem.title=self.productModel.name;
    
    NSString *urlStr=[NSString stringWithFormat:@"http://m.beequick.cn/show/productDetail?id=%@&shopId=7917",self.productModel.product_id];
    //1.创建网址
    NSURL *url=[NSURL URLWithString:urlStr];
    //2.创建请求
    NSURLRequest *request=[NSURLRequest requestWithURL:url];
    
    //3.加载请求
    [self.webView loadRequest:request];
    
    [self setCollectionAndShare];
    
    //将控件显示在所有控件的最上面
    [self.view bringSubviewToFront:self.shopCartButton];
    
    
    
    //设置进度条
    NSTimer *timer=[NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(updateProgess) userInfo:nil repeats:YES];
    
    self.timer=timer;
    [[NSRunLoop mainRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];
    
    
}
-(void)updateProgess
{
    self.progessView.progress+=0.1;
    if (self.progessView.progress>=0.8) {
        [self.timer invalidate];
    }
}


//设置收藏和分享
-(void)setCollectionAndShare
{
    UIButton *btnShare=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    [btnShare setBackgroundImage:[UIImage imageNamed:@"goods_share"] forState:UIControlStateNormal];
    UIButton  *btnSave=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    [btnSave setBackgroundImage:[UIImage imageNamed:@"non_collection"] forState:UIControlStateNormal];
    UIBarButtonItem *rightItem=[[UIBarButtonItem alloc]initWithCustomView:btnSave];
    UIBarButtonItem *rightItem_1=[[UIBarButtonItem alloc]initWithCustomView:btnShare];
    
    NSArray *rightArr=[NSArray arrayWithObjects:rightItem_1,rightItem , nil];
    self.navigationItem.rightBarButtonItems=rightArr;
    
    
    
    
}

//减号点击事件
- (IBAction)reduceBtn:(id)sender {
    
    UIButton *reduceBtn=(UIButton *)sender;
    
    int num=[self.numberLabel.text intValue]-1;
    
    if (num<0) {
        reduceBtn.enabled=YES;
        [reduceBtn setBackgroundImage:[UIImage imageNamed:@"v2_reduce"] forState:UIControlStateNormal];
        [self.increaseButton setBackgroundImage:[UIImage imageNamed:@"v2_increase"] forState:UIControlStateNormal];
    }else{
        self.numberLabel.text=@(num).stringValue;
        
    }
    
    
}
//加号点击事件
- (IBAction)increaseBtn:(id)sender {
    UIButton *increaseBtn=(UIButton *)sender;
    
    int num=[self.numberLabel.text intValue]+1;
    self.numberLabel.text=@(num).stringValue;
    self.increaseButton.enabled=num<self.productModel.store_nums;
    if (num>0) {
        [increaseBtn setBackgroundImage:[UIImage imageNamed:@"v2_increased"] forState:UIControlStateNormal];
        [self.reduceButton setBackgroundImage:[UIImage imageNamed:@"v2_reduced"] forState:UIControlStateNormal];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark --webview的代理

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    [self.timer invalidate];
    self.progessView.hidden=YES;
}



@end
